c42dbd88f648b1d26bd2af6345d108139d44edbe,src/main/java/common/comment/CommentsInserter.java,CommentsInserter,updateDb,#List#,125
Before Change
statement.executeBatch();
}
}
statement.executeBatch();
}
catch (SQLException e)
{
After Change
public int updateDb(List<Comment> comments)
{
int rowsUpdated = 0;
final int batchSize = 100;
int count = 0;
Connection connection = DbManager.getConnection();
String query = "INSERT INTO Comment " +
"(id, post_id, message, created_at, from_id, from_name, likes, replies, parent_id) " +
"VALUES (?,?,?,?,?,?,?,?,?) " +
"ON DUPLICATE KEY UPDATE post_id=VALUES(post_id),message=VALUES(message),likes=VALUES(likes),replies=VALUES(replies),parent_id=VALUES(parent_id)";
PreparedStatement statement = null;
try
{
statement = connection.prepareStatement(query);
for(Comment comment: comments)
{
statement.setString(1, comment.getId());
statement.setString(2, comment.getPostId());
statement.setString(3, comment.getMessage());
statement.setString(4, Util.toDbDateTime(comment.getCreatedAt()));
statement.setString(5, comment.getFromId());
statement.setString(6, comment.getFromName());
statement.setInt(7, comment.getLikes());
statement.setInt(8, comment.getReplies());
statement.setString(9, comment.getCommentId());
statement.addBatch();
if(++count % batchSize == 0)
{
int[] tempUpdated = statement.executeBatch();
for(int i: tempUpdated)
{
rowsUpdated += i;
}
}
}
int[] tempUpdated = statement.executeBatch();
for(int i: tempUpdated)
{
rowsUpdated += i;
}
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
if(null != statement) try { statement.close(); } catch (SQLException e) { e.printStackTrace(); }
if(null != connection) try { connection.close(); } catch (SQLException e) { e.printStackTrace(); }
}
return rowsUpdated;
}
}